/*
 * @Author: kanglang
 * @Date: 2021-02-23 11:22:57
 * @LastEditors: kanglang
 * @LastEditTime: 2021-04-29 22:07:51
 * @Description: 企业资料
 */

import React from 'react';
import { View, StyleSheet, Text, SafeAreaView, TouchableOpacity, BackHandler } from 'react-native';
import { msg, XMHeader, BackImage, cache, AsyncStorage } from '@/common';
import { px2dp, mainBgColorLightGray, isAndroid, fontColorCoffee } from '@/styles';
import RowItem from '@/components/SimpleRowItem';
import businessApi from '@/api/business';
// import AsyncStorage from '@react-native-community/async-storage';

export default class BusinessData extends React.Component {
  constructor(props) {
    super(props);
    const { navigation } = props;
    const state = navigation.state || {};
    const params = state.params || {};
    const { routerFrom } = params;
    this.state = {
      infoState: 0, // 企业信息证信息认证：：3, 审核中,1通过,2拒绝
      idCardState: 0, // 法人身份证信息认证：：3, 审核中,1通过,2拒绝
      bankCardState: 0, // 对公账户认证：3, 审核中,1通过,2拒绝
      adminState: 0, // 管理员认证：3, 审核中,1通过,2拒绝
      routerFrom
    };
  }

  componentDidMount() {
    AsyncStorage.getItem(cache.CURRENT_LEGAL).then((val) => {
      let currentLegal = JSON.parse(val);
      this.setState({ ...currentLegal }, () => {
        const { custId } = this.state;
        businessApi.getLegalState({ custId }).then(res => {
          if (res.success) {
            this.setState({
              ...res.data
            });
          }
        });
      });
    });
    if (isAndroid) {
      BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
    }
  }

  componentWillUnmount() {
    if (isAndroid) {
      BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
    }
  }

    onBackAndroid = () => {
      msg.emit('router: refreshRoute', {
        routeName: 'B2BMallTab',
      });
      msg.emit('router: back');
      return true;
    }

    render() {
      const { infoState, idCardState, bankCardState, adminState } = this.state;

      const infoStatus = (state) => {
        if (state == 1) {
          return {
            type: '已认证',
            color: '#9D9A95'
          };
        }
        if (state == 2) {
          return {
            type: '认证失败',
            color: '#EB2F2F'
          };
        }
        if (state == 3) {
          return {
            type: '待审核',
            color: '#EF9730'
          };
        }
        return '';
      };
      return (
        <SafeAreaView style={styles.container}>
          <XMHeader
            title='企业资料'
            renderLeft={() => {
              return (
                <TouchableOpacity
                  activeOpacity={0.8}
                  style={isAndroid ? { paddingVertical: px2dp(14), paddingHorizontal: px2dp(40) } :
                    { paddingVertical: px2dp(14), paddingRight: px2dp(40) }}
                  onPress={() => {
                    msg.emit('router: refreshRoute', {
                      routeName: 'B2BMallTab',
                    });
                    msg.emit('router: back');
                  }}
                >
                  <BackImage />
                </TouchableOpacity>
              );
            }}
            renderRight={() => {
              return (
                <>
                  {
                    infoState == 1 && idCardState == 1 && bankCardState == 1 && adminState == 1 ?
                      <TouchableOpacity
                        activeOpacity={0.8}
                        style={styles.headerLeft}
                        onPress={this.addLegal}
                      >
                        <Text style={{ marginHorizontal: 6, fontSize: px2dp(32), color: fontColorCoffee }}> + 添加企业</Text>
                      </TouchableOpacity>
                      : null
                  }
                </>
              );
            }}
          />
          <View style={styles.inner}>
            <RowItem
              titleRight={infoStatus(infoState).type}
              rightColor={infoStatus(infoState).color}
              title='企业信息认证'
              onPress={this.toBusinessCerti}
            />
            <RowItem
              titleRight={infoStatus(idCardState).type}
              rightColor={infoStatus(idCardState).color}
              title='法人信息认证'
              onPress={this.toLegalCerti}
            />
            <RowItem
              titleRight={infoStatus(bankCardState).type}
              rightColor={infoStatus(bankCardState).color}
              title='对公账户认证'
              onPress={this.toAccountCerti}
            />
            <RowItem
              titleRight={infoStatus(adminState).type}
              rightColor={infoStatus(adminState).color}
              title='管理员授权认证'
              onPress={this.toManagerCerti}
            />
          </View>
          {
            infoState == 3 && idCardState == 3 && bankCardState == 3 && adminState == 3 &&
            <View style={styles.tipBox}>
              <Text style={styles.star}>*</Text>
              <Text style={styles.tips}>您的企业认证资料已经提交，提交后预估在1-3个工作日内会审核完成，请您耐心等待。</Text>
            </View>
          }
        </SafeAreaView>
      );
    }

    addLegal = () => {
      const { routerFrom } = this.state;
      msg.emit('router: goToNext', {
        routeName: 'CertiKnow',
        routerFrom
      });
    }

    // 企业认证
    toBusinessCerti = () => {
      msg.emit('router: goToNext', {
        routeName: 'B2BBusinessInfoVerify'
      });
    }

    // 法人认证
    toLegalCerti = () => {
      msg.emit('router: goToNext', {
        routeName: 'B2BLegalInfoVerify'
      });
    }

    // 对公账户认证
    toAccountCerti = () => {
      msg.emit('router: goToNext', {
        routeName: 'B2BCorporateAccountInfoVerify'
      });
    }

    // 管理员认证
    toManagerCerti = () => {
      msg.emit('router: goToNext', {
        routeName: 'B2BBusinessManagerSetVerify'
      });
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: mainBgColorLightGray,
  },
  inner: {
    marginTop: px2dp(25)
  },
  tipBox: {
    flexDirection: 'row',
    marginTop: px2dp(24),
    paddingLeft: px2dp(32),
    paddingRight: px2dp(35)
  },
  star: {
    color: '#EB2F2F',
    fontSize: px2dp(28)
  },
  tips: {
    color: '#9D9A95',
    fontSize: px2dp(28),
    marginLeft: px2dp(8)
  }

});
